In this lecture, we're going to take a look at a service called the Messaging Service.
This is a very simple service that allows you to send messages between servers.
And it only has two functions.
One of these functions is to listen to a particular topic and receive messages from it.
It's called subscribe async and it says this function registers a callback to begin listening to the
given topic.
The callback is invoked when a topic receives a message.
It can be called multiple times for the same topic, and it says the callback is invoked with a single
argument.
A table with the following two entries.
It has a key called data and it says developer supplied payload and then sent and it says unit time
in seconds at which the message was sent.
And then the other function we have is the ability to send messages to a particular topic.
So this one's called publish async and it says this function sends the provided message to all subscribers
to the topic triggering their register callbacks to be invoked.
So the messaging service allows servers of the same experience to communicate with each other in real
time, less than one second using topics.
Topics are developer defined strings 1 to 80 characters that serve as used to send and receive messages.
Delivery is best effort and not guaranteed.
Make sure to architect your experience so delivery failures are not critical.
They also give us some information about the different limitations with messages.
For example, what the maximum size of a message can be, how many messages you can send per minute.
How many messages can be received per minute per topic, how many messages can be received for an entire
game, the subscriptions allowed per game server and subscribe requests per game server.
There are also some other limitations with the messaging service that we're going to take a look at
in a moment, but basically, the service, as it says, allows you to send information between different
servers.
This is how you communicate or have cross server messaging.
So inside of studio I've made a new place.
This is just called my messaging service demonstration, and I've published it to Roblox and I've made
the server count only one.
So that way I can have two separate accounts hopping into two separate servers.
So that way we can see the messages being sent between them in action.
I have a script and server script service.
I'll just call it messaging service.
And let's go ahead and grab the messaging service.
So it's going to be game git service messaging service.
And we can go ahead and take a look at the two functions that it has.
Now the first function we're going to take a look at is subscribe.
Async begins listening to a given topic.
So we need to define whatever topic we would like.
For this I'm just going to call it my player message topic because I want it to receive information.
When a player my game sends a chat message.
So we're going to call it player message, but you can call it whatever you'd like.
And then we have to give it a callback function.
So we'll pass a lambda function here.
And if you remember the documentation this function is given an argument.
That's a table that contains two keys.
One is called data and one is called sent.
So I will just call this my um I guess I'll just call this my message and I'll type annotate it as a
table that has a key of data.
And this could be anything.
And then it also has a key called sent which is a Unix timestamp.
So that's going to be a number.
And now what I want to do is I want to be able to print what the message is inside of the console.
So I'm going to print whenever this callback is invoked I'm going to print that we received a message.
And that message is going to be inside of our message table.
And we're going to print data as well as when this message was sent.
And now to send messages to different servers.
What I'm going to do is I'm also going to grab the player service.
And I want to go ahead and listen for when a player is added into my game, because I want to listen
for when this player chats.
So we're going to do player dot chatted.
We're going to connect a function.
We're going to get that message.
And to demonstrate some of the limitations with the messaging service, we're going to have some predefined
messages here.
So for example if let's say our player messages, let's say they say something like, um, I kind of
want this to be like command.
So we could do exclamation mark.
And the first example we'll do here is mixed.
So what I'm going to do when a player chats this is I'm going to try and attempt to send a mixed table
through the messaging service.
So we're going to do messaging service publish async.
We need to refer to what topic we want to publish this message to.
In this case that's our player message topic.
So we'll copy that there.
And the message that I want to send is I want to send a mixed table or a table that has both indexes
and keys inside of it.
So inside of this table I'll have an index of one.
We could set that to something like a string of hello.
But then I also want to have keys.
So we'll have key one.
We'll have this one say howdy and then we'll have key number two.
And this one will be called hi.
Now if a player sends a different message let's say they say, um, let's try to pass a function through
the messaging service.
So we could do messaging service.
And actually let's just copy what we've done here.
Paste this here.
And then we're going to try to pass a function through the messaging service.
This will just be some kind of simple, uh, lambda function will pass through here.
We'll see if that works.
Um, what else do we want to do?
Let's go ahead and try to see if we can pass a meta table through the messaging service.
So we'll do we'll call this one meta table.
And again we'll just copy this paste this here.
And we'll call the set meta table function to return a new table.
So we'll just have an empty table.
Actually let's put some keys in and we'll call this key one is equal to.
We'll just say meta.
And then this one key two we'll just say hahaha whatever.
And then we want to attach a meta table to this.
And this meta table is just going to have some kind of key in it.
We'll just call it uh key two.
And we'll just say table something like that.
So let's go ahead and try to pass a table with a meta table attached to it through the messaging service.
What else should we try.
Um, let's do it for the other data types.
So let's do it for like strings.
We should be able to send strings through the messaging service.
So again I'm just going to copy this paste this here.
And let's go ahead and send a message of like hello there.
Let's check to see if the player says uh they want to send a number.
So we'll copy this, paste this here.
We'll just pass a number like one two, three.
And then finally let's go ahead and try to send a boolean to the messaging service.
So we'll just copy this paste this there and we'll send the value of true.
Now of course, the subscribe async and the publish async are asynchronous functions, meaning they
have the chance to error.
So you're more than likely going to want to wrap these function calls inside of a p call or a protected
call.
I'm not going to do it here just because I'm not going to waste my time with that.
This is just a demonstration, but let's go ahead and publish these changes.
We'll publish this to Roblox, and then I'm going to hop into this game with two different accounts,
so we can see this messaging service in action.
And actually before I do that, um, I need to account for the fact that when we print messages out,
like, for example, this is a table, it's only going to print the location of the table in memory
inside of the console, because when you're doing it in studio, studio gives it some nice extra formatting
so you can actually see what's inside of the table.
But unfortunately, when we're actually in the Roblox player using the developer console, it's not
going to show up that way.
So if the type of the value of this message we receive.
So we could do if message dot data, if the type of that is a table, then let's just go ahead and loop
through every single key value pair inside of the table.
So in pairs message dot data.
This will work for both key value pairs and index value pairs.
It'll just print everything.
And then we can just go ahead and print out the key and the value.
Now I don't think that we'll be able to send a mixed tables through the messaging service.
And just to confirm this, let's go ahead and print out inside of this table to see if it has a key
one.
And also let's check to see if this table has a key number two.
And then to check to see if this table has a meta table.
We can just print something like um get meta table.
We'll get the meta table for this particular table.
And we're only going to do that if this table has that key one inside of it of meta.
So this way we can tell if we actually had the table passed to us that contained the meta table, if
it has uh, this value stored in key one.
So if message dot data key one, if that's equal to meta, then we know this is supposed to be the table
that has the meta table attached to it.
So we'll print to see if it actually has a meta table after being passed through the messaging service.
Okay.
Let's publish those changes.
And now let's go ahead and hop into the Roblox player and test it out.
Okay, so here I am in my game on two separate accounts.
And what I'm going to do is I'm going to open up the F9 menu, and we're going to go to the server side
of the console, and we're going to do the same thing for my other account.
And let's go ahead and see if we are able to receive messages on this other server.
So the first command we could go ahead and try out is let's go ahead and try out the string command.
So we're going to chat string.
And as you can see, both servers receive the message of hello there and it gives us the timestamp of
when that message was sent on both of the servers.
So you can see that both of them, since they were both listening to that exact same topic, which was
the player message topic, they both received the message of hello there!
Very cool.
Now let's go ahead and try it out with a number.
We'll do exclamation mark number.
And there you go.
Both servers receive that message and the data was indeed one, two, three for both servers.
Okay.
Let's go ahead and try out sending a boolean.
So we'll do a boolean.
Hey look at that.
We received the message on both servers as well which was the value of true.
Okay let's get a little bit more fancy.
Let's go ahead and try sending a mixed table.
So we're going to do exclamation mark mixed.
And what are you going to see.
Well it did receive a table.
But if you take a look at that it only got that first index value pair in the table which was the index
of one and the value of hello with it, meaning those other two keys in the table, key one and key
two did not exist.
So this is one of the limitations with the messaging service.
You cannot send mixed tables through the publish async function.
Let's go ahead and try using let's say um let's try to send a meta table through the messaging service.
So if we do exclamation mark meta table you're going to see that we do get the keys for the table printed
out.
But if you notice when we try to print out the meta table itself we got nil.
So that's another limitation with the messaging service.
Any tables that have meta tables attached to them those meta tables will be lost.
So this is basically the same thing as losing, um, meta tables through remote events and losing mixed
table data through remote events.
You can't send them through remote events and you can't send them through the messaging service either.
Let's go ahead and try a function.
Let's send a function through the messaging service.
And we're going to see is we get an error cannot publish function, can only accept valid UTF eight
characters.
So as you can see we cannot send functions through the messaging service either.
This one actually caused an error.
And that's basically all there is to the messaging service.
It's a very simple service that only has two functions.
One of the functions listens to a particular topic and is given a table that contains the data for that
particular topic.
When we use the publish async function and when that message was sent.
And then of course we use the publish async message to send a message to a particular topic, whatever
values we want, as long as they aren't functions, we'll lose meta tables through the publishing service,
and we can't send mixed tables through the messaging service either.
And of course, you can't send other stuff like custom Roblox data types or different things like for
example, you can't send instances through the messaging service.
And actually, just to have a little fun, let's go ahead and try to send some kind of Roblox data type
through the messaging service.
Like, let's try to send, uh, a color, a color three data type, and let's go ahead and see what
happens.
We're going to publish it to player message and we'll just pass a new color.
Three let's just do a white color.
And let's go ahead and see what happens when we try to publish this through the messaging service.
So I'll publish changes.
All right, we'll do exclamation mark color, and we should.
There we go.
We get the same error.
Cannot publish color three can only accept valid UTF eight characters.
So you are not able to send custom Roblox data types through the messaging service either.
Hopefully this lecture has given you a better understanding of the messaging service and how to use
its two functions.
This should open up many possibilities in your games.
For example, maybe you could create global announcements across multiple servers in your game.
You could create some kind of real time server browser, or even some kind of cross server chat, where
other players can chat with other players that are in completely separate servers.
Thank you for watching and I'll see you in the next lecture.